package main

import (
	"fmt"
	"code.google.com/p/odie"
)

func hotwordHandler(w *odie.ResponseWriter, req *odie.Request, vars odie.Context) {

	hotword := vars.Get("hotword")

	if hotword == "" {
		return
	}

	fmt.Fprintf(w, "OK, I'll tell you when someone says '%s'.", hotword)
	
	go func () {

		m := odie.NewMatcher()

		m.Add(hotword, func (w2 *odie.ResponseWriter, req *odie.Request, vars odie.Context) {

			// only do this once
			m.Cancel()

			name := req.Context.Get("name")

			if name == "" {
				name = "someone"
			}

			fmt.Fprintf(w, "ALERT %s said '%s'", name, req.Query)
		})

		m.SubscribeAndServe(nil)
	} ()
}


func init () {

	odie.Handle("tell me when someone says $hotword", hotwordHandler) 
}

                
